package com.haochang.chunkvod.controller.activity.welcome;

import java.util.ArrayList;
import java.util.List;

import com.haochang.base.BaseActivity;
import com.haochang.base.BaseListView;
import com.haochang.base.OnBaseClickListener;
import com.haochang.chunkvod.R;
import com.haochang.chunkvod.app.common.VodConstant;
import com.haochang.chunkvod.app.common.intent.IntentCode;
import com.haochang.chunkvod.app.common.intent.IntentKey;
import com.haochang.chunkvod.app.utils.CollectionUtils;
import com.haochang.chunkvod.app.utils.HelperUtils;
import com.haochang.chunkvod.app.utils.ToastUtils;
import com.haochang.chunkvod.app.utils.XmlResourceParserUtil;
import com.haochang.chunkvod.controller.adapter.base.BaseAdapterHelper;
import com.haochang.chunkvod.controller.adapter.base.QuickAdapter;
import com.haochang.chunkvod.model.hostess.HostessData;
import com.haochang.chunkvod.model.hostess.IpInfo;
import com.haochang.chunkvod.view.vod.title.TitleView;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;

/**
 * 选择城市或门店
 *
 * @author Yangjie
 *         className ChooseCityActivity
 *         created at  2016/11/11  14:18
 */
public class ChooseCityAndShopActivity extends BaseActivity
{
	private HostessData hostessData;
	private QuickAdapter<IpInfo> adapter;
	private List<IpInfo> ipInfoList = new ArrayList<IpInfo>();

	private enum ChooseType
	{
		CHOOSE_CITY, CHOOSE_SHOP
	}

	ChooseType chooseType = ChooseType.CHOOSE_CITY;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
	}

	@Override
	public void receiveParam()
	{
		chooseType = TextUtils.equals(IntentKey.IP_CHOOSE_SHOP, getIntent().getStringExtra(IntentKey.IP_CHOOSE_SHOP)) ? ChooseType.CHOOSE_SHOP
				: ChooseType.CHOOSE_CITY;
	}

	@Override
	public void initData()
	{
		if (chooseType == ChooseType.CHOOSE_CITY)
			hostessData = new HostessData(ChooseCityAndShopActivity.this);
	}

	@Override
	public void initViews()
	{
		setContentView(R.layout.activity_choose_city);
		BaseListView listView = (BaseListView) findViewById(R.id.listView);
		TitleView titleView = (TitleView) findViewById(R.id.titleView);
		titleView.setTitle(chooseType == ChooseType.CHOOSE_SHOP ? R.string.choose_shop : R.string.choose_city).setRightIconVISIBLE(false)
				.setTitleOnClickListener(new TitleView.ITitleOnClickListener()
				{
					@Override
					public void onBackClick()
					{
						finish();
						animRightToLeft();
					}

					@Override
					public void onDoneClick()
					{
					}
				});
		adapter = new QuickAdapter<IpInfo>(ChooseCityAndShopActivity.this, R.layout.item_choose_city_and_shop, ipInfoList)
		{
			@Override
			protected void convert(BaseAdapterHelper helper, IpInfo item)
			{
				helper.setText(R.id.tv_title, chooseType == ChooseType.CHOOSE_CITY ? item.getCity() : item.getShop());
				if (chooseType == ChooseType.CHOOSE_SHOP)
				{
					if (TextUtils.equals(HelperUtils.getHelperInstance().getSValue(VodConstant.IP_CHOOSED_SHOP, ""), item.getShop())
							&& TextUtils.equals(HelperUtils.getHelperInstance().getSValue(VodConstant.IP_CHOOSED_CITY, ""), item.getCity()))
					{
						helper.getView(R.id.iv_right).setVisibility(View.VISIBLE);
						((ImageView) helper.getView(R.id.iv_right)).setImageResource(R.drawable.ic_gou);
						helper.setTextColor(R.id.tv_title, context.getResources().getColor(R.color.red));
					} else
					{
						helper.getView(R.id.iv_right).setVisibility(View.INVISIBLE);
						helper.setTextColor(R.id.tv_title, context.getResources().getColor(R.color.color_282828));
					}
				} else
				{
					((ImageView) helper.getView(R.id.iv_right)).setImageResource(R.drawable.right_arrows);
					helper.setTextColor(R.id.tv_title, context.getResources().getColor(R.color.color_282828));
				}
				helper.getView().setTag(R.id.choose_city_item_id, item);
				helper.getView().setOnClickListener(onBaseClickListener);
			}
		};
		listView.setAdapter(adapter);
		if (chooseType == ChooseType.CHOOSE_SHOP)
		{//这是选择门店
			IpInfo ipInfo = getIntent().getParcelableExtra(IntentKey.IP_CHOOSE_SHOP_ITEM);
			if (null != ipInfo)
			{
				ipInfoList = ipInfo.getArrayList();
				adapter.replaceAll(ipInfoList);
			}
		} else
		{
			//选择城市
			initLocalIpConfig();//本地ip配置方案
			//            requestCityList();//从网络获取ip配置
		}
	}

	OnBaseClickListener onBaseClickListener = new OnBaseClickListener()
	{
		@Override
		public void onBaseClick(View v)
		{
			if (chooseType == ChooseType.CHOOSE_SHOP)
			{//点击选择门店
				IpInfo ipInfo = (IpInfo) v.getTag(R.id.choose_city_item_id);
				Intent intent = getIntent();
				intent.putExtra(IntentKey.IP_CHOOSE_SHOP_ITEM, ipInfo);
				setResult(RESULT_OK, intent);
				finish();
			} else
			{
				//点击城市item跳转到选择门店
				IpInfo ipInfo = (IpInfo) v.getTag(R.id.choose_city_item_id);
				Intent intent = new Intent(ChooseCityAndShopActivity.this, ChooseCityAndShopActivity.class);
				intent.putExtra(IntentKey.IP_CHOOSE_SHOP, IntentKey.IP_CHOOSE_SHOP);
				intent.putExtra(IntentKey.IP_CHOOSE_SHOP_ITEM, ipInfo);
				startActivityForResult(intent, IntentCode.IP_CHOOSE_SHOP);
				animLeftToRight();
			}
		}
	};

	private void initLocalIpConfig()
	{
		ipInfoList = XmlResourceParserUtil.getInstance().parseXML();
		XmlResourceParserUtil.getInstance().releaseInstance();
		adapter.replaceAll(ipInfoList);
	}

	/**
	 * 获取预收list
	 */
	@SuppressWarnings("unchecked")
	private void requestCityList()
	{
		hostessData.requestIpconfigList("", new HostessData.ResultResponseList()
		{
			@Override
			public void onRequestSuccess(List list)
			{
				if (!CollectionUtils.isEmpty(list))
				{
					ipInfoList.clear();
					adapter.replaceAll(list);
				}
			}

			@Override
			public void onFail(int errno, String errMsg)
			{
				//				ToastUtils.showText(ChooseCityAndShopActivity.this, TextUtils.isEmpty(errMsg) ? "获取门店列表失败!" : errMsg);
				ToastUtils.showText(ChooseCityAndShopActivity.this, "获取门店列表失败，将启用本地配置!");
				initLocalIpConfig();
			}
		});
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data)
	{
		super.onActivityResult(requestCode, resultCode, data);
		if (resultCode == RESULT_OK && requestCode == IntentCode.IP_CHOOSE_SHOP)
		{
			if (data != null)
			{
				IpInfo ipInfo = data.getParcelableExtra(IntentKey.IP_CHOOSE_SHOP_ITEM);
				Intent intent = getIntent();
				intent.putExtra(IntentKey.IP_CHOOSE_SHOP_ITEM, ipInfo);
				setResult(RESULT_OK, intent);
				finish();
				animRightToLeft();
			}
		}
	}
}
